package com.example.springstudy.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.example.springstudy.dao.dto.OrderDTO;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@TableName("order_r")
@Data
@NoArgsConstructor
public class Order extends BaseEntity{
    private int shopId;

    private String orderNo;

    private Long customerId;

    private int employeeId;

    private int totalFee;

    private int amount;

    private String device;

    private int payType;

    private String attach;

    private int status;

    private Date timeStart;
    private Date timeExpire;

    private Long createBy;
    private Long updateBy;

    public Order(OrderDTO orderDTO) {
        this.shopId = orderDTO.getShopId();
        this.orderNo = orderDTO.getOrderNo();
        this.customerId = orderDTO.getCustomerId();
        this.setId(orderDTO.getOrderId());
        this.employeeId = orderDTO.getEmployeeId();
        this.payType = orderDTO.getPayType();
        this.status = orderDTO.getStatus();

        List<OrderSku> list = orderDTO.getSkuList();
        this.amount = list.stream()
                .map(item -> item.getSalePrice() * item.getSkuCount())
                .reduce(0, Integer::sum);
        this.totalFee = list.stream()
                .map(item -> item.getUnitPrice() * item.getSkuCount())
                .reduce(0, Integer::sum);
    }
}
